﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Windows.Forms;
using System.Collections.Concurrent;

namespace Twin
{
    public class Device
    {
        public SerialPort port;

        public String sPortLabel;
        public String sPortBaudrate;

        //public SerialIn dataIn = new SerialIn();;
        //public String sBufOut = "";
        public String sBufIn = "";
        
        
        public long lastRxSec = 0;
        public bool fChannelReady = false;


        private byte inst_index;

        public Queue<string> queueTxMsg = new Queue<string>();
        public Queue<string> queueRxMsg = new Queue<string>();



        


        public Device(String devPortName){

            //sPortLabel = OperateIniFile.ReadIniData(devPortName, "Com", "", ConfigIO.SensorMonitor_ini);
            //sPortBaudrate = OperateIniFile.ReadIniData(devPortName, "Baudrate", "", ConfigIO.SensorMonitor_ini);


            //port = new SerialPort("COM" + sPortLabel, Convert.ToInt32(sPortBaudrate));



            //关键 为 sensor绑定事件句柄
            //port.DataReceived += new SerialDataReceivedEventHandler(PortDataReceived);



            /*
            sensor.port = new SerialPort("COM" + sComSensor, Convert.ToInt32(sBaudrateSensor));
            term.port = new SerialPort("COM" + sComTerminal, Convert.ToInt32(sBaudrateTerminal));



            //关键 为 sensor绑定事件句柄
            sensor.port.DataReceived += new SerialDataReceivedEventHandler(SensorDataReceived);

            //关键 为 sensor绑定事件句柄
            term.port.DataReceived += new SerialDataReceivedEventHandler(TermDataReceived);


            sComSensor = OperateIniFile.ReadIniData("OptionSensor", "Com", "", ConfigIO.SensorMonitor_ini);
            sBaudrateSensor = OperateIniFile.ReadIniData("OptionSensor", "Baudrate", "", ConfigIO.SensorMonitor_ini);
            Status.Items[0].Text = "     ";
            Status.Items[0].Text += "Sensor COM" + sComSensor + " : " + sBaudrateSensor;


            sComTerminal = OperateIniFile.ReadIniData("OptionTerminal", "Com", "", ConfigIO.SensorMonitor_ini);
            sBaudrateTerminal = OperateIniFile.ReadIniData("OptionTerminal", "Baudrate", "", ConfigIO.SensorMonitor_ini);
            Status.Items[1].Text = "                                                                                                            ";
            Status.Items[1].Text += "Terminal COM" + sComTerminal + " : " + sBaudrateTerminal; ;
            */

        }

        public bool Open(){
            bool fSuccess = true;
            try { port.Open(); }
            catch (Exception)
            {
                fSuccess = false;
            }
            return fSuccess;

            /*
            String sReturn = "串口: COM" + sPortLabel + " 打开成功\r\n"; ;
            try{ port.Open(); }
            catch (Exception){
                sReturn = "串口: COM" + sPortLabel + " 打开失败\r\n"; }
            return sReturn;*/

        }

        public void resetLastRxSec(){
            lastRxSec = getTotalSeconds();
        }

        private long getTotalSeconds(){
            DateTime dt = DateTime.Parse("01/01/1970");
            TimeSpan ts = DateTime.Now - dt;
            long sec = (long)ts.TotalSeconds;
            return sec;
        }

        private bool isTimeover(){
            long sec = getTotalSeconds();
            long delta = sec - lastRxSec;

            if (delta >= 3) return true;
            return false;
        }


        //如果收到数据，表示TxChannel活跃
        public bool isChannelReady(){
            if (fChannelReady == false)return false;
            if (isTimeover() == true)return false;
            return true;
        }


        public byte getInstIndex(){
            inst_index++;
            saveDisk(inst_index);
            return inst_index;
        }

        private void saveDisk(byte inst_index)
        {
            //
        }


        //Echo
        /*public void Echo(KMsg8 m, String sEcho){
            KMsg8 echo = new KMsg8();
        
            echo.setOp(m.getOp());
            echo.setBitPP(KConst.PP.bitINST_DIR, false);      //
            echo.setIndex(m.getIndex());      //

            String[] segs = Make.MsgText(echo, sEcho);  //"RD|CFG|" + FileOut);
            SegsToPort(segs);
        }
        */



        public void PushTo_InstList(string[] inst){

            queueTxMsg.Clear();
            for (int i = 0; i < inst.Length; i++){
                if (inst[i].Trim().Equals("") == false)
                {
                    queueTxMsg.Enqueue(inst[i]);
                }
            }
        }
    }
}
